//
//  StressDetailViewModel.swift
//  DaRings
//
//  Created by HFY on 2022/11/13.
//

import UIKit
import SwiftDate
import MYModule

open class StressProvider: NSObject {
    
    public var detail: StressDetailModel?
    
    public init(date: Date?) {
        super.init()
        
        guard let date = date else {
            return
        }

        let stressModel = SingleStressModel.last(from: date.timeIntervalSince1970)
        detail(of: stressModel)
       
    }
    
   public init(stressModel: SingleStressModel?) {
        super.init()
        
        detail(of: stressModel)
    }
    
    open func detail(of stressModel: SingleStressModel?) {
        
        let detail = StressDetailModel()
        
        guard let stressModel = stressModel else {
            
            detail.stress = 0
            detail.date = "--"
            let emptyData = (0 ..< 7).map { _ in SingleStressModel() }
            detail.sevenTimesStress = emptyData
            self.detail = detail
            
            return
        }

        let date = Date(timeIntervalSince1970: stressModel.end)
        detail.date = date.localStringOfDateTime
        detail.stress = stressModel.stress
        
        let stressModels = SingleStressModel.sevenTimesModel(from: stressModel.end)
          
        detail.sevenTimesStress = stressModels.reversed()
        
        self.detail = detail
        
    }
    
}
